﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;
public partial class Manager_OrderApp_CreateOrderApp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Ajax.Utility.RegisterTypeForAjax(typeof(Manager_OrderApp_CreateOrderApp));
        if (!IsPostBack)
        {
            BindTopicList();
            BindCatalogueByTopicID();
            BindStoreList();
            BinData();
        }
        Session["grv_Master"] = grv_Master;
    }
    private void BindTopicList()
    {
        clsWebShop cls = new clsWebShop();
        ListItem item = new ListItem();
        ddlChuDe.DataSource = cls.Get_TopicNoneProc().Tables[0];
        ddlChuDe.DataTextField = "TopicName";
        ddlChuDe.DataValueField = "TopicID";
        ddlChuDe.DataBind();
        item.Text = "-- Chọn chủ đề --";
        item.Value = "0";
        ddlChuDe.Items.Insert(0, item);
        FindValueDLL(ddlChuDe, Request.QueryString["sTopicID"]);
    }

    private void BindStoreList()
    {
        clsWebShop cls = new clsWebShop();
        ListItem item = new ListItem();
        ddlStore.DataSource = cls.Get_StoreByKindNoNoneProc().Tables[0];
        ddlStore.DataTextField = "StName";
        ddlStore.DataValueField = "StID";
        ddlStore.DataBind();
        item.Text = "-- Chọn kho --";
        item.Value = "0";
        ddlStore.Items.Insert(0, item);
        ddlStore.SelectedItem.Selected = false;

        if (Request.QueryString["StID"] != "" && Request.QueryString["StID"] != null)
            ddlStore.Items.FindByValue(Request.QueryString["StID"]).Selected = true;
    }

    private void BindCatalogueByTopicID()
    {
        clsWebShop cls = new clsWebShop();

        ListItem item = new ListItem();
        DataTable dt = null;
        dt = cls.Get_CatalogueByTopicIDNoneProc(ddlChuDe.SelectedValue).Tables[0];
        ddlTheLoai.DataSource = dt;
        ddlTheLoai.DataTextField = "Catname";
        ddlTheLoai.DataValueField = "CatID";
        ddlTheLoai.DataBind();
        item.Text = "-- Chọn thể loại--";
        item.Value = "0";
        ddlTheLoai.Items.Insert(0, item);
        FindValueDLL(ddlTheLoai, Request.QueryString["sCatID"]);
    }

    private void FindValueDLL(DropDownList ddl, string value)
    {
        if (value != "" && value != null)
            ddl.Items.FindByValue(value).Selected = true;
    }
    private void BinData()
    {
        PccMsg msg = new PccMsg();
        string KindNo = "";
        if (Request.QueryString["KindNo"] != "" && Request.QueryString["KindNo"] != null)
            KindNo = Request.QueryString["KindNo"];
        msg.CreateFirstNode("PdName", txtTenSP.Text.Trim());
        msg.CreateFirstNode("CatID", ddlTheLoai.SelectedValue);
        msg.CreateFirstNode("TopicID", ddlChuDe.SelectedValue);
        msg.CreateFirstNode("StID", ddlStore.SelectedValue);
        msg.CreateFirstNode("KindNo", KindNo);
        clsWebShop cls = new clsWebShop();
        DataSet ds = null;
        ds = cls.Get_ProductByCondition(msg.GetXmlStr);
        grv_Master.DataSource = ds;
        grv_Master.DataBind();

    }
    
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BinData();
    }
    protected void ddlChuDe_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindCatalogueByTopicID();
        BinData();
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtTenSP.Text = "";
        ddlChuDe.SelectedIndex = 0;
        ddlStore.SelectedIndex = 0;
        ddlTheLoai.SelectedIndex = 0;
        BinData();
    }
    protected void grv_Master_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.className='cursorOverGrid'");
            if (e.Row.RowIndex % 2 == 0)
                e.Row.Attributes.Add("onmouseout", "this.className='gvRowStyle'");
            else
                e.Row.Attributes.Add("onmouseout", "this.className='gvAlternatingRowStyle'");
        }
    }
    protected void grv_Master_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv_Master.PageIndex = e.NewPageIndex;
        BinData();
    }
    [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
    public string AjaxGetPD(string sPDName, string sTopic, string sCatID, string sStID)
    {
       PccMsg msg = new PccMsg();

        msg.CreateFirstNode("PdName", sPDName.Trim());
        msg.CreateFirstNode("CatID", sCatID);
        msg.CreateFirstNode("TopicID", sTopic);
        msg.CreateFirstNode("StID", sStID);
        msg.CreateFirstNode("KindNo", "");
        clsWebShop cls = new clsWebShop();
        DataSet ds = null;
        ds = cls.Get_ProductByCondition(msg.GetXmlStr);
        GridView grv_Master1 = (GridView)Session["grv_Master"];
        grv_Master1.DataSource = ds;
        grv_Master1.DataBind();
        return GetControlRenderString(grv_Master1);

    }

    /*string GetControlRenderString(System.Web.UI.Control ctrl)
    {
        string ret = "";
        System.IO.StringWriter stringWriter = new System.IO.StringWriter();
        HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
        try
        {
            ctrl.RenderControl(htmlWriter);
            ret = stringWriter.ToString().Replace("\n", "").Replace("\t", "").Replace("\r", "");
        }
        catch (Exception e)
        {
            ret = e.Message;
        }
        return ret;
    }*/

    string GetControlRenderString(System.Web.UI.Control ctrl)
    {
        string ret = "";
        System.IO.StringWriter stringWriter = new System.IO.StringWriter();
        HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
        try
        {
            HtmlForm frm = new HtmlForm();
            frm.Controls.Add(ctrl);
            ctrl.RenderControl(htmlWriter);
            ret = stringWriter.ToString().Replace("\n", "").Replace("\t", "").Replace("\r", "");
        }
        catch (Exception e)
        {
            ret = stringWriter.ToString().Replace("\n", "").Replace("\t", "").Replace("\r", ""); 
        }
        return ret;
    }
  
    protected void btnAddBasket_Click(object sender, EventArgs e)
    {
        
    }
}